package control;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.admin.AdminService;
import model.gallery.BestWeeklyVO;
import model.member.MemberVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class AdminController extends MultiActionController {

	private AdminService adminService;

	public AdminController() {
		super();
		// TODO Auto-generated constructor stub
	}

	public AdminController(AdminService adminService) {
		super();
		this.adminService = adminService;
	}

	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) throws SQLException {
		String write = request.getParameter("page");
		String path = "index";
		HttpSession session = request.getSession(false);
		if (session != null && session.getAttribute("mvo") != null) {
			path = write + ".admin";
			// System.out.println("session 있음");
		}
		return new ModelAndView(path);
	}

	// 1번탭 컨트롤러 메서드
	public ModelAndView addWeek(HttpServletRequest request,
			HttpServletResponse response, BestWeeklyVO vo) throws SQLException {
		adminService.addWeek(vo);
		return new ModelAndView("main.admin");
	}

	// 2번탭 컨트롤러 메서드
	public ModelAndView updateBest(HttpServletRequest request,
			HttpServletResponse response) throws SQLException {
		int week = Integer.parseInt(request.getParameter("pri"));

		BestWeeklyVO vo = adminService.getWeeklyInfo(week);
		adminService.updateBest(vo);
		System.out.println("updateBest.." + vo);
		return new ModelAndView("main.admin");
	}

	public ModelAndView getUpdateList(HttpServletRequest request,
			HttpServletResponse response) throws SQLException {

		List<BestWeeklyVO> list = adminService.getUpdateList();

		return new ModelAndView("admin/updateBest", "list", list);
	}
	

	public ModelAndView getMemberList(HttpServletRequest request, HttpServletResponse response) throws SQLException {

		String page = request.getParameter("page");
		return new ModelAndView("admin/updateMember", "mList", adminService.getMemberList(page));
	}
	
	public ModelAndView pagingMember(HttpServletRequest request, HttpServletResponse response) throws SQLException {
		String page = request.getParameter("page");
		return new ModelAndView("admin/pagingMember", "mList", adminService.getMemberList(page));
	}
	
	public ModelAndView lvChange(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		
		int uid = Integer.parseInt(request.getParameter("uid"));
		int lv = Integer.parseInt(request.getParameter("lv"));
		
		 adminService.lvChange(uid, lv);
		  
		  System.out.println("Controller : "+ uid + " " + lv);
		  
		  
		  return new ModelAndView("main.admin");// 화면이 넘어갔다 오기 때문에, 타일즈로 처리해야 한다. 
	}

	public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws SQLException {
		int uId = Integer.parseInt(request.getParameter("uId"));
		return new ModelAndView();

	}

	public ModelAndView leave(HttpServletRequest request, HttpServletResponse response) throws SQLException {
		int uId = Integer.parseInt(request.getParameter("uId"));
		adminService.leave(uId);
		return new ModelAndView("main.admin");
	}
	
	public ModelAndView findMember(HttpServletRequest request,
			HttpServletResponse response) throws SQLException {
		return new ModelAndView("admin/findMember");

	}
	
	public ModelAndView findMemberList(HttpServletRequest request,
			HttpServletResponse response) throws SQLException {
			String nickName=request.getParameter("nickName");
			//System.out.println(nickName);
			List<MemberVO> mvo=adminService.findMemberList(nickName);
			//System.out.println("control"+mvo);
			/*for(int i=0;i<mvo.size();i++){
				System.out.println(mvo.get(i).getId());
			}*/
		return new ModelAndView("JsonView","mvo",mvo);

	}
	
	public ModelAndView deleteWeekly(HttpServletRequest request,
			HttpServletResponse response) throws SQLException {
		int week = Integer.parseInt(request.getParameter("pri"));
			BestWeeklyVO vo = adminService.getWeeklyInfo(week);
			adminService.nullset(vo);
			adminService.deleteWeekly(week);
			return new ModelAndView("main.admin");

	}
	
}
















